﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;
using CoachingOrganizer.Toolkit.Events;

namespace CoachingOrganizer.Android.Adapters.Tools.MoneyCalculation
{
    public class MoneyFlowItemNameSuggestionsAdapter : BaseAdapter, IFilterable
    {
        private readonly Activity _context;
        private readonly MoneyFlowModel _model;
        private readonly Lazy<MoneyFlowItemNameSuggestionsFilter> _fitler;
        private readonly bool _forStableItems;
        private List<MoneyFlowItemNameSuggestionModel> _items;
        private List<MoneyFlowItemNameSuggestionModel> _filteredItems;

        public IEnumerable<MoneyFlowItemNameSuggestionModel> Items
        {
            get { return _items = _forStableItems ? EnumerateStableItemsNameSuggestions().ToList() : EnumerateOccasionalItemsNameSuggestions().ToList(); }
        }

        public MoneyFlowItemNameSuggestionsAdapter(Activity context, MoneyFlowModel model, bool forStableItems)
            : base()
        {
            _context = context;
            _model = model;
            _fitler = new Lazy<MoneyFlowItemNameSuggestionsFilter>(() => new MoneyFlowItemNameSuggestionsFilter(this));
            _filteredItems = _items = forStableItems ? EnumerateStableItemsNameSuggestions().ToList() : EnumerateOccasionalItemsNameSuggestions().ToList();
            _forStableItems = forStableItems;
        }

        public Filter Filter
        {
            get { return _fitler.Value; }
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return _filteredItems.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(Resource.Layout.MoneyFlowItemNameSuggestionLayout, parent, false);
            HookModelToView(view, _filteredItems[position]);
            return view;
        }

        public void SetFilter(IEnumerable<string> values)
        {
            _filteredItems = _items.Where(i => values.Contains(i.SuggestedName)).ToList();
            NotifyDataSetChanged();
        }

        private void HookModelToView(View view, MoneyFlowItemNameSuggestionModel moneyFlowItemNameSuggestionModel)
        {
            TextView suggestedNameTextView = view.FindViewById<TextView>(Resource.Id.suggestedNameTextView);
            suggestedNameTextView.Text = moneyFlowItemNameSuggestionModel.SuggestedName;
            view.Click += (sender, e) => moneyFlowItemNameSuggestionModel.ActivateSuggestion();
        }

        private IEnumerable<MoneyFlowItemNameSuggestionModel> EnumerateStableItemsNameSuggestions()
        {
            ActionResult<IEnumerable<MoneyFlowItemNameSuggestionModel>> result = _model.EnumerateStableItemsNameSuggestions();
            return result.IsValid ? result.Result : new MoneyFlowItemNameSuggestionModel[0];
        }

        private IEnumerable<MoneyFlowItemNameSuggestionModel> EnumerateOccasionalItemsNameSuggestions()
        {
            ActionResult<IEnumerable<MoneyFlowItemNameSuggestionModel>> result = _model.EnumerateOccasionalItemsNameSuggestions();
            return result.IsValid ? result.Result : new MoneyFlowItemNameSuggestionModel[0];
        }
    }
}